﻿using System.Collections.Generic;
using System.Linq;
using AccelerateSensor.Database.SQLite;

namespace AccelerateSensor.Service.DbProxy.SQLite.Dal
{
    internal class NodeCommonAttribute
    {
        private static readonly string TableName = SqliteHelper.TableNodeCommonAttribute;

        /// <summary>
        /// 添加节点公有属性
        /// </summary>
        /// <param name="model">节点公有属性对象</param>
        /// <returns>Rid</returns>
        public int Insert(Models.NodeCommonAttribute model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(NodeUuid, SleepTimeSec, SendStatusIntervalMin, " +
                      "     PowerSource, WorkMode, SyncEnable, " +
                      "     PowerVoltage, NodeTemp, PowerSaving) " +
                      "values(@NodeUuid, @SleepTimeSec, @SendStatusIntervalMin, " +
                      "     @PowerSource, @WorkMode, @SyncEnable, " +
                      "     @PowerVoltage, @NodeTemp, @PowerSaving);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加节点公有属性
        /// </summary>
        /// <param name="models">节点公有属性对象集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.NodeCommonAttribute> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(NodeUuid, SleepTimeSec, SendStatusIntervalMin, " +
                      "     PowerSource, WorkMode, SyncEnable, " +
                      "     PowerVoltage, NodeTemp, PowerSaving) " +
                      "values(@NodeUuid, @SleepTimeSec, @SendStatusIntervalMin, " +
                      "     @PowerSource, @WorkMode, @SyncEnable, " +
                      "     @PowerVoltage, @NodeTemp, @PowerSaving);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新节点公有属性(不修改节点公有属性Id)
        /// </summary>
        /// <param name="model">节点公有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.NodeCommonAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set NodeUuid = @NodeUuid, SleepTimeSec = @SleepTimeSec, SendStatusIntervalMin = @SendStatusIntervalMin, " +
                      "     PowerSource = @PowerSource, WorkMode = @WorkMode, SyncEnable = @SyncEnable, " +
                      "     PowerVoltage = @PowerVoltage, NodeTemp = @NodeTemp, PowerSaving = @PowerSaving " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 通过节点编号更新节点公有属性
        /// </summary>
        /// <param name="model">节点公有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateByNodeUuid(Models.NodeCommonAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set SleepTimeSec = @SleepTimeSec, SendStatusIntervalMin = @SendStatusIntervalMin, " +
                      "     PowerSource = @PowerSource, WorkMode = @WorkMode, SyncEnable = @SyncEnable, " +
                      "     PowerVoltage = @PowerVoltage, NodeTemp = @NodeTemp, PowerSaving = @PowerSaving " +
                      "where NodeUuid = @NodeUuid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除节点公有属性
        /// </summary>
        /// <param name="model">节点公有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.NodeCommonAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"delete from {TableName} where Rid = @Rid";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询节点公有属性
        /// </summary>
        /// <param name="rid">节点公有属性Id</param>
        /// <returns>节点公有属性对象</returns>
        public Models.NodeCommonAttribute GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.NodeCommonAttribute() { Rid = rid };
            return SqliteDbHelper.GetDataList<Models.NodeCommonAttribute>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询节点公有属性
        /// </summary>
        /// <param name="nodeUuid">节点Id</param>
        /// <returns>节点公有属性对象</returns>
        public Models.NodeCommonAttribute GetModel(string nodeUuid)
        {
            var sql = $"select * from {TableName} where NodeUuid = @NodeUuid";
            var model = new Models.NodeCommonAttribute() { NodeUuid = nodeUuid };
            return SqliteDbHelper.GetDataList<Models.NodeCommonAttribute>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询所有节点公有属性
        /// </summary>
        /// <returns>节点公有属性对象集合</returns>
        public List<Models.NodeCommonAttribute> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.NodeCommonAttribute>(sql);
        }
    }
}
